﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Trackr.Core.Entities
{
	public class StopWatch
	{
		public StopWatch()
		{
		}
		public void Start()
		{
			if (this.IsRunning)
				this.Stop();
			StartTime = DateTime.Now;
			StopTime = null;
			IsRunning = true;
		}
		public void Stop()
		{
			if (IsRunning == false)
				return;
			StopTime = DateTime.Now;
			IsRunning = false;
		}
		public bool IsRunning { get; private set; }
		public DateTime StartTime { get; private set; }
		public DateTime? StopTime { get; private set; }
		public TimeSpan Duration
		{
			get
			{
				var stopTime = StopTime.HasValue ? StopTime.Value : DateTime.Now;
				return stopTime.Subtract(StartTime);
			}
		}


		public void Clear()
		{
			this.IsRunning = false;
			this.StopTime = DateTime.MinValue;
			this.StartTime = DateTime.MinValue;
		}
	}
}
